(function() {

this.codium = this.codium || {};
this.codium.panel = this.codium.panel || {};

this.codium.panel.Ticket = codium.panel.BasicPanel.extend(
{
	init : function() 
	{
		this._super();
		
		this.setIcon(this.libDir + "/img/icon/menuTicket.png");
		this.setCaption("Project Ticket");
		
		this.service = new codium.CodiumService();
		
		this.pageControl = new codium.control.PageControl();
		this.pageControl.setLeft(10);
		this.pageControl.setTop(40);
		this.pageControl.setWidth(this.getWidth() - (2 * this.pageControl.getLeft()));
		this.pageControl.setHeight(this.getHeight() - this.pageControl.getTop() - this.pageControl.getLeft());
		this.addControl(this.pageControl);
		
			this.pageMy = new codium.control.Page();
			this.pageMy.setCaption("My Ticket");
			this.pageMy.setTabWidth(100);
			this.pageControl.addControl(this.pageMy);
			
				this.listMy = new codium.control.TicketList();
				this.listMy.setLeft(15);
				this.listMy.setTop(15);
				this.listMy.setWidth(this.pageMy.getWidth() - (2 * this.listMy.getLeft()));
				this.listMy.setHeight(this.pageMy.getHeight() - this.listMy.getTop() - 50);
				this.pageMy.addControl(this.listMy);
				
				this.btnMyOpenTicket = new pasta.webgui.Button();
				this.btnMyOpenTicket.setLeft(this.listMy.getLeft());
				this.btnMyOpenTicket.setTop(this.listMy.getTop() + this.listMy.getHeight() + 5);
				this.btnMyOpenTicket.setCaption("Open");
				this.btnMyOpenTicket.setWidth(80);
				this.btnMyOpenTicket.getOnClick().setHandler(this, this.btnMyOpenTicketClick);
				this.pageMy.addControl(this.btnMyOpenTicket);
				
				this.btnMyNewTicket = new pasta.webgui.Button();
				this.btnMyNewTicket.setLeft(this.btnMyOpenTicket.getLeft() + this.btnMyOpenTicket.getWidth() + 3);
				this.btnMyNewTicket.setTop(this.btnMyOpenTicket.getTop());
				this.btnMyNewTicket.setCaption("New Ticket");
				this.btnMyNewTicket.setWidth(95);
				this.btnMyNewTicket.getOnClick().setHandler(this, this.btnMyNewTicketClick);
				this.pageMy.addControl(this.btnMyNewTicket);
				
				this.btnMyRefresh = new pasta.webgui.Button();
				this.btnMyRefresh.setLeft(this.btnMyNewTicket.getLeft() + this.btnMyNewTicket.getWidth() + 3);
				this.btnMyRefresh.setTop(this.btnMyNewTicket.getTop());
				this.btnMyRefresh.setCaption("Refresh");
				this.btnMyRefresh.setWidth(80);
				this.btnMyRefresh.getOnClick().setHandler(this, this.btnMyRefreshClick);
				this.pageMy.addControl(this.btnMyRefresh);
				
			this.pageActive = new codium.control.Page();
			this.pageActive.setCaption("Active Ticket");
			this.pageActive.setTabWidth(100);
			this.pageControl.addControl(this.pageActive);
			
				this.listActive = new codium.control.TicketList();
				this.listActive.setLeft(15);
				this.listActive.setTop(15);
				this.listActive.setWidth(this.pageActive.getWidth() - (2 * this.listActive.getLeft()));
				this.listActive.setHeight(this.pageActive.getHeight() - this.listActive.getTop() - 50);
				this.pageActive.addControl(this.listActive);
				
				this.btnActiveOpenTicket = new pasta.webgui.Button();
				this.btnActiveOpenTicket.setLeft(this.listActive.getLeft());
				this.btnActiveOpenTicket.setTop(this.listActive.getTop() + this.listActive.getHeight() + 5);
				this.btnActiveOpenTicket.setCaption("Open");
				this.btnActiveOpenTicket.setWidth(80);
				this.btnActiveOpenTicket.getOnClick().setHandler(this, this.btnActiveOpenTicketClick);
				this.pageActive.addControl(this.btnActiveOpenTicket);
				
				this.btnActiveNewTicket = new pasta.webgui.Button();
				this.btnActiveNewTicket.setLeft(this.btnActiveOpenTicket.getLeft() + this.btnActiveOpenTicket.getWidth() + 3);
				this.btnActiveNewTicket.setTop(this.btnActiveOpenTicket.getTop());
				this.btnActiveNewTicket.setCaption("New Ticket");
				this.btnActiveNewTicket.setWidth(95);
				this.btnActiveNewTicket.getOnClick().setHandler(this, this.btnMyNewTicketClick);
				this.pageActive.addControl(this.btnActiveNewTicket);
				
				this.btnActiveRefresh = new pasta.webgui.Button();
				this.btnActiveRefresh.setLeft(this.btnActiveNewTicket.getLeft() + this.btnActiveNewTicket.getWidth() + 3);
				this.btnActiveRefresh.setTop(this.btnActiveNewTicket.getTop());
				this.btnActiveRefresh.setCaption("Refresh");
				this.btnActiveRefresh.setWidth(80);
				this.btnActiveRefresh.getOnClick().setHandler(this, this.btnMyRefreshClick);
				this.pageActive.addControl(this.btnActiveRefresh);
				
			this.pageClosed = new codium.control.Page();
			this.pageClosed.setCaption("Closed Ticket");
			this.pageClosed.setTabWidth(100);
			this.pageControl.addControl(this.pageClosed);
			
				this.listClosed = new codium.control.TicketList();
				this.listClosed.setLeft(15);
				this.listClosed.setTop(15);
				this.listClosed.setWidth(this.pageClosed.getWidth() - (2 * this.listClosed.getLeft()));
				this.listClosed.setHeight(this.pageClosed.getHeight() - this.listClosed.getTop() - 50);
				this.pageClosed.addControl(this.listClosed);
				
				this.btnClosedOpenTicket = new pasta.webgui.Button();
				this.btnClosedOpenTicket.setLeft(this.listClosed.getLeft());
				this.btnClosedOpenTicket.setTop(this.listClosed.getTop() + this.listClosed.getHeight() + 5);
				this.btnClosedOpenTicket.setCaption("Open");
				this.btnClosedOpenTicket.setWidth(80);
				this.btnClosedOpenTicket.getOnClick().setHandler(this, this.btnClosedOpenTicketClick);
				this.pageClosed.addControl(this.btnClosedOpenTicket);
				
				this.btnClosedNewTicket = new pasta.webgui.Button();
				this.btnClosedNewTicket.setLeft(this.btnClosedOpenTicket.getLeft() + this.btnClosedOpenTicket.getWidth() + 3);
				this.btnClosedNewTicket.setTop(this.btnClosedOpenTicket.getTop());
				this.btnClosedNewTicket.setCaption("New Ticket");
				this.btnClosedNewTicket.setWidth(95);
				this.btnClosedNewTicket.getOnClick().setHandler(this, this.btnMyNewTicketClick);
				this.pageClosed.addControl(this.btnClosedNewTicket);
				
				this.btnClosedRefresh = new pasta.webgui.Button();
				this.btnClosedRefresh.setLeft(this.btnClosedNewTicket.getLeft() + this.btnClosedNewTicket.getWidth() + 3);
				this.btnClosedRefresh.setTop(this.btnClosedNewTicket.getTop());
				this.btnClosedRefresh.setCaption("Refresh");
				this.btnClosedRefresh.setWidth(80);
				this.btnClosedRefresh.getOnClick().setHandler(this, this.btnMyRefreshClick);
				this.pageClosed.addControl(this.btnClosedRefresh);
		
		this.ticketList = new Array();
	},	
	
	initPanel : function()
	{
		this.refreshMy();
	},
	
	refreshMy : function()
	{
		var target = this;
		this.service.listTicket(app.projectId, function(result) {target.updateTicket(result);});
	},
	
	updateTicket : function(list)
	{
		this.ticketList = list;
		this.listMy.clear();
		this.listActive.clear();
		this.listClosed.clear();
		
		for (var i = 0; i < list.length; i++)
		{
			var obj = list[i];
			
			if (parseInt(obj.state) != 6)
			{
				this.listActive.addItem(obj.id, obj.no, obj.ticketTitle, obj.assigneeUserName, obj.state);
				
				if (obj.assigneeUserId == app.userObj.userId)
					this.listMy.addItem(obj.id, obj.no, obj.ticketTitle, obj.assigneeUserName, obj.state);
			}
			else
			{
				this.listClosed.addItem(obj.id, obj.no, obj.ticketTitle, obj.assigneeUserName, obj.state);
			}
		}
	},
	
	openTicketItem : function(object)
	{
		if (this.pnlNewTicket == null)
		{
			this.pnlTicket = new codium.panel.TicketItem();
			this.pnlTicket.setWidth(stage.getWidth());
			this.pnlTicket.setHeight(stage.getHeight());
			this.pnlTicket.getOnChangeState().setHandler(this, this.pnlTicketChangeState);
			stage.addControl(this.pnlTicket);
			
			this.pnlNewTicket = new codium.panel.NewTicket();
			this.pnlNewTicket.getOnOK().setHandler(this, this.pnlNewTicketOK);
			this.pnlNewTicket.setWidth(stage.getWidth());
			this.pnlNewTicket.setHeight(stage.getHeight());
			stage.addControl(this.pnlNewTicket);
		}
		
		this.pnlTicket.show(object);
	},
	
	pnlTicketChangeState : function(sender, id, newState, msg)
	{
		var target = this;
		this.service.ticketChangeState(id, newState, msg,
			function(result)
			{
				var statusString = "Created";
				var status = parseInt(result);
				
				switch (status)
				{
					case 1 :
							statusString = "Created";
							break;
					case 2 :
							statusString = "Accepted";
							break;
					case 3 :
							statusString = "In Progress";
							break;
					case 4 :
							statusString = "Rejected";
							break;
					case 5 :
							statusString = "Completed";
							break;
					case 6 :
							statusString = "Closed";
							break;
					default :
							statusString = "Undefined";
							break;
				}
				
				alert("Ticket state has been changed to " + statusString);
				
				target.refreshMy(); 
				target.pnlTicket.close();
			});
	},
	
	//-------------------- Service Event Handler --------------
	
	btnMyOpenTicketClick : function()
	{
		if (this.listMy.getSelectedItem() != null)
		{
			for (var i = 0; i < this.ticketList.length; i++)
			{
				var obj = this.ticketList[i];
				
				if (obj.id == this.listMy.getSelectedItem().id)
				{
					this.openTicketItem(obj);
					break;
				}
			}
		}
	},
	
	btnActiveOpenTicketClick : function()
	{
		if (this.listActive.getSelectedItem() != null)
		{
			for (var i = 0; i < this.ticketList.length; i++)
			{
				var obj = this.ticketList[i];
				
				if (obj.id == this.listActive.getSelectedItem().id)
				{
					this.openTicketItem(obj);
					break;
				}
			}
		}
	},
	
	btnClosedOpenTicketClick : function()
	{
		if (this.listClosed.getSelectedItem() != null)
		{
			for (var i = 0; i < this.ticketList.length; i++)
			{
				var obj = this.ticketList[i];
				
				if (obj.id == this.listClosed.getSelectedItem().id)
				{
					this.openTicketItem(obj);
					break;
				}
			}
		}
	},
	
	btnMyNewTicketClick : function(sender)
	{
		if (this.pnlNewTicket == null)
		{
			this.pnlTicket = new codium.panel.TicketItem();
			this.pnlTicket.setWidth(stage.getWidth());
			this.pnlTicket.setHeight(stage.getHeight());
			this.pnlTicket.getOnChangeState().setHandler(this, this.pnlTicketChangeState);
			stage.addControl(this.pnlTicket);
			
			this.pnlNewTicket = new codium.panel.NewTicket();
			this.pnlNewTicket.getOnOK().setHandler(this, this.pnlNewTicketOK);
			this.pnlNewTicket.setWidth(stage.getWidth());
			this.pnlNewTicket.setHeight(stage.getHeight());
			stage.addControl(this.pnlNewTicket);
		}
		
		this.pnlNewTicket.show();
	},
	
	btnMyRefreshClick : function(sender)
	{
		this.refreshMy();
	},
	
	pnlNewTicketOK : function(sender, title, description, userId, priority, duedate)
	{
		var target = this;
		
		this.service.addTicket(app.projectId, app.userObj.userId, title, description, userId, priority, duedate, 
								function(result) 
								{
									target.refreshMy(); 
									alert("Ticket hash been send to " + target.pnlNewTicket.getUserName());
								});
	},
	
	setWidth : function(data)
	{
		this._super(data);
		
		if (this.pageControl != null)
		{
			this.pageControl.setWidth(this.getWidth() - (2 * this.pageControl.getLeft()));
			
			this.listMy.setWidth(this.pageMy.getWidth() - (2 * this.listMy.getLeft()));
			
			this.listActive.setWidth(this.pageMy.getWidth() - (2 * this.listActive.getLeft()));
			
			this.listClosed.setWidth(this.pageMy.getWidth() - (2 * this.listClosed.getLeft()));
		}
	},
	
	setHeight : function(data)
	{
		this._super(data);
		
		if (this.pageControl != null)
		{
			this.pageControl.setHeight(this.getHeight() - this.pageControl.getTop() - this.pageControl.getLeft());
			
			this.listMy.setHeight(this.pageMy.getHeight() - this.listMy.getTop() - 50);
			this.btnMyOpenTicket.setTop(this.listMy.getTop() + this.listMy.getHeight() + 5);
			this.btnMyNewTicket.setTop(this.btnMyOpenTicket.getTop());
			this.btnMyRefresh.setTop(this.btnMyOpenTicket.getTop());
			
			this.listActive.setHeight(this.pageActive.getHeight() - this.listActive.getTop() - 50);
			this.btnActiveOpenTicket.setTop(this.listActive.getTop() + this.listActive.getHeight() + 5);
			this.btnActiveNewTicket.setTop(this.btnActiveOpenTicket.getTop());
			this.btnActiveRefresh.setTop(this.btnActiveOpenTicket.getTop());
			
			this.listClosed.setHeight(this.pageMy.getHeight() - this.listClosed.getTop() - 50);
			this.btnClosedOpenTicket.setTop(this.listClosed.getTop() + this.listClosed.getHeight() + 5);
			this.btnClosedNewTicket.setTop(this.btnClosedOpenTicket.getTop());
			this.btnClosedRefresh.setTop(this.btnClosedOpenTicket.getTop());
		}
	},
	
});

})();